package com.yuan.controller;

import com.yuan.config.RabbitConfig;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;


@RequestMapping("/send")
@RestController
public class SendMessageController {
    @Autowired
    RabbitTemplate rabbitTemplate;
    @GetMapping("/msgA")
    public String sendDirectMessageA(String msg){
        String messageId = String.valueOf(System.currentTimeMillis());
        String messageData=msg;
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        rabbitTemplate.convertAndSend(RabbitConfig.EXCHANGE_A,RabbitConfig.ROUTINGKEY_A,map);
        return "ok";
    }
    @GetMapping("/msgB")
    public String sendDirectMessageB(String msg){
        String messageId = String.valueOf(System.currentTimeMillis());
        String messageData=msg;
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        rabbitTemplate.convertAndSend(RabbitConfig.EXCHANGE_A,RabbitConfig.ROUTINGKEY_B,map);
        return "ok";
    }
    @GetMapping("/msg")
    public String sendDirectMessage(String msg){
        String messageId = String.valueOf(System.currentTimeMillis());
        String messageData=msg;
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        rabbitTemplate.convertAndSend(RabbitConfig.FANOUT_EXCHANGE,"",map);
        return "ok";
    }
}
